package org.jeecg.modules.engineering.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.engineering.entity.EquipFaceInfo;
import org.jeecg.modules.engineering.entity.EquipFaceRecord;
import org.jeecg.modules.engineering.entity.EquipFaceUser;
import org.jeecg.modules.engineering.service.IEquipFaceInfoService;
import org.jeecg.modules.engineering.service.IEquipFaceRecordService;
import org.jeecg.modules.engineering.service.IEquipFaceUserService;
import org.jeecg.modules.engineering.service.IEquipTelpoFaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Slf4j
@Api(tags="app")
@RestController
@RequestMapping("/b/app/engineering")
public class EngineeringAppController {

    @Autowired
    private IEquipFaceInfoService equipFaceInfoService;

    @Autowired
    private IEquipFaceUserService equipFaceUserService;

    @Autowired
    private IEquipFaceRecordService equipFaceRecordService;

    @Autowired
    private IEquipTelpoFaceService equipTelpoFaceService;


    /**
     * 查询天波人脸识别设备列表
     * @return
     */
    @PostMapping(value = "/queryFaceEquipList")
    public Result<?> queryFaceEquipList() {
        List<EquipFaceInfo> infos = equipFaceInfoService.list();
        return Result.ok(infos);
    }

    /**
     * 向天波人脸识别设备中添加或者修改人员
     * @return
     */
    @PostMapping(value = "/addUserToFaceEquip")
    public Result<?> addUserToFaceEquip(EquipFaceUser telpoFaceUser) {
        return equipTelpoFaceService.addOrUpdateUser(telpoFaceUser.getEquipSn(), Collections.singletonList(telpoFaceUser) );
    }

    /**
     * 查询设备或者人员的刷脸记录
     * @return
     */
    @PostMapping(value = "/queryFaceRecordList")
    public Result<?> queryFaceRecordList(EquipFaceRecord equipFaceRecord, HttpServletRequest req) {
        if(StringUtils.isBlank(equipFaceRecord.getEquipSn()) && StringUtils.isBlank(equipFaceRecord.getPin())){
            return Result.error("设备SN或者人员PIN至少传一个");
        }
        QueryWrapper<EquipFaceRecord> queryWrapper = QueryGenerator.initQueryWrapper(equipFaceRecord, req.getParameterMap());
        List<EquipFaceRecord> records = equipFaceRecordService.list(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        if(records != null && records.size() > 0){
            for (EquipFaceRecord record : records){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("equipId",record.getEquipId());
                jsonObject.put("equipSn",record.getEquipSn());
                jsonObject.put("recordId",record.getRecordId());
                jsonObject.put("pin",record.getPin());
                jsonObject.put("name",record.getName());
                jsonObject.put("photo",record.getPhoto());
                jsonObject.put("time",record.getTime());
                jsonObject.put("verifyType",record.getVerifyType());
                jsonArray.add(jsonObject);
            }
        }
        return Result.ok(jsonArray);
    }
}
